package com.fangyi.yunmusic.service.impl;

import com.fangyi.yunmusic.config.ResourceConfig;
import com.fangyi.yunmusic.exception.YunMusicException;
import com.fangyi.yunmusic.mapper.UserFollowMapper;
import com.fangyi.yunmusic.mapper.UserIsArtistMapper;
import com.fangyi.yunmusic.pojo.UserFollow;
import com.fangyi.yunmusic.pojo.UserIsArtist;
import com.fangyi.yunmusic.pojo.detail.UserDetail;
import com.fangyi.yunmusic.pojo.vo.UserVO;
import com.fangyi.yunmusic.service.UserService;
import com.fangyi.yunmusic.mapper.UserMapper;
import com.fangyi.yunmusic.pojo.User;
import com.fangyi.yunmusic.utils.MD5Utils;
import com.fangyi.yunmusic.utils.PojoUtils;
import org.n3r.idworker.Id;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Autowired
    private UserFollowMapper userFollowMapper;

    @Autowired
    private UserIsArtistMapper userIsArtistMapper;

    @Autowired
    private ResourceConfig resourceConfig;




    @Override
    public Boolean checkUserIsArtist(String userId) {
        Example example = new Example(UserIsArtist.class);
        example.createCriteria().andEqualTo("userId", userId);
        UserIsArtist userIsArtist = userIsArtistMapper.selectOneByExample(example);
        return userIsArtist != null;
    }

    @Override
    public User selectUserById(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }
     @Override
    public void deleteUserById(String userId) {
        userMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public void addUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public void updateUser(User user) {
        User userForUpdate = userMapper.selectByPrimaryKey(user.getId());
        PojoUtils.copyProperties(user, userForUpdate);
        userMapper.updateByPrimaryKey(userForUpdate);
    }

    @Override
    public boolean userRegister(String nickname,String password,String description) {
        User user=new User();
        String md5Paswword=null;
        try {
            md5Paswword=MD5Utils.getMD5Str(password);
        } catch (Exception e) {

        }
        String id = Id.nextString();
        user.setId(id);
        user.setUserType(0);
        user.setNickname(nickname);
        user.setPassword(md5Paswword);
        user.setDescription(description);
        user.setAvatarImgId("109951163280978723");
        if(userMapper.insertSelective(user)==0){
            throw new YunMusicException("注册失败");
        }
        return true;
    }

    @Override
    public String userLogin(String nickName, String password) {
        User user=userMapper.selectUserByNickname(nickName);
        if(user!=null) {
            String md5Passworld = null;
            try {
                md5Passworld = MD5Utils.getMD5Str(password);
            } catch (Exception e) {

            }
            if(user.getPassword().equals(md5Passworld)) return user.getId();
            else throw new YunMusicException("密码错误");
        }
        return null;
    }

    @Override
    public boolean updatePassword(String userId,String oldPassword,String newPassword) {
        User user=userMapper.selectByPrimaryKey(userId);
        String md5OldPassworld=null;
        String md5NewPassworld=null;
        try {
            md5OldPassworld = MD5Utils.getMD5Str(oldPassword);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(md5OldPassworld.equals(user.getPassword())) {
            try {
                md5NewPassworld = MD5Utils.getMD5Str(newPassword);
            } catch (Exception e) {

            }
            user.setPassword(md5NewPassworld);
            if(userMapper.updateByPrimaryKey(user)==0){
                throw new YunMusicException("修改失败");
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean updateDescription(String userId,String description) {
        User user=userMapper.selectByPrimaryKey(userId);
        user.setDescription(description);
        if(userMapper.updateByPrimaryKey(user)==0){
            throw new YunMusicException("修改失败");
        }
        return true;
    }

    @Override
    public UserDetail getUserMessage(String userId) {

        List<UserFollow> list_follow=null;
        List<UserFollow> list_followed=null;

        User user=userMapper.selectByPrimaryKey(userId);

        list_follow=userFollowMapper.selectUserFollowByFollowId(userId);
        list_followed=userFollowMapper.selectUserFollowByFollowedId(userId);

        UserDetail userDetail=new UserDetail();

        userDetail.setNumberOfFollow(list_follow.size());
        userDetail.setNumberOfFollower(list_followed.size());
        userDetail.setId(user.getId());
        userDetail.setNickname(user.getNickname());
        userDetail.setDescription(user.getDescription());
        userDetail.setAvatarImgId(user.getAvatarImgId());
        userDetail.setUserType(user.getUserType());
        userDetail.setAvatarImgUrl(resourceConfig.getUserAvatarUrl(user.getAvatarImgId()));

        return userDetail;
    }

    @Override
    public boolean verifyUsernameRepetition(String nickname) {
       /* List<User> userList = userMapper.selectAll();
        for (User user : userList) {
            if (nickname.equals(user.getNickname())) return false;
        }*/
       User user=userMapper.selectUserByNickname(nickname);
       if(user!=null) return false;
        return true;
    }

    @Override
    public UserVO queryUserForLogin(String nickname, String password) throws Exception{
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("nickname", nickname);
        User user = userMapper.selectOneByExample(example);
        if (user == null) {
            return null;
        }
        String md5Password = MD5Utils.getMD5Str(password);
        if (user.getPassword().equals(md5Password)) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user, userVO);
            userVO.setAvatarImgUrl(resourceConfig.getUserAvatarUrl(user.getAvatarImgId()));

            Example userIsArtistExample = new Example(UserIsArtist.class);
            userIsArtistExample.createCriteria().andEqualTo("userId", user.getId());
            UserIsArtist userIsArtist = userIsArtistMapper.selectOneByExample(userIsArtistExample);
            if (userIsArtist != null) {
                userVO.setArtistId(userIsArtist.getArtistId());
            }

            return userVO;
        }
        return null;
    }
}
